package com.useeinfo.oa.modules.security.entity;

import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.extend.entity.SuperClassEntity;
import com.useeinfo.framework.sugar.tools.CommonSugar;
import com.useeinfo.framework.sugar.tools.DateTimeUtils;
import com.useeinfo.oa.modules.base.entity.Enterprise;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

/**
 * 系统角色表
 */
@Entity
@Table(name = "system_role")
public
class SystemRole extends SuperClassEntity<SystemRole> {

    private String roleName;

    private String roleDescription;

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "system_role_2_authority", joinColumns = @JoinColumn(name = "role_id"), inverseJoinColumns = @JoinColumn(name = "authority_id"))
    private Set<SystemAuthority> authoritySet = new HashSet<>();
    private boolean systemManager;
    private boolean enabled;

    /**
     * 所属企业
     */
    @ManyToOne
    @JoinColumn(name = "enterprise_id")
    private Enterprise enterprise;


    @Override
    public JSONObject toJSONObject() {
        JSONObject jsonObject = new JSONObject();
        Enterprise enterprise = CommonSugar.getTypedDefault(getEnterprise(), new Enterprise());
        jsonObject.put("roleId", getId());
        jsonObject.put("createTime", CommonSugar.getTypedDefault(DateTimeUtils.formatDateToStringWithTime(getCreateDate()), ""));
        jsonObject.put("roleName", CommonSugar.getTypedDefault(getRoleName(), ""));
        jsonObject.put("roleDescription", CommonSugar.getTypedDefault(getRoleDescription(), ""));
        jsonObject.put("systemManager", isSystemManager());
        jsonObject.put("enterpriseName", enterprise.getEnterpriseName());
        jsonObject.put("enabled", isEnabled() ? "是" : "否");

        return jsonObject;
    }

    public SystemRole() {
    }

    public SystemRole(Long roleId) {
        setId(roleId);
    }

    public String getRoleName() {
        return roleName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }

    public String getRoleDescription() {
        return roleDescription;
    }

    public void setRoleDescription(String roleDescription) {
        this.roleDescription = roleDescription;
    }

    public Set<SystemAuthority> getAuthoritySet() {
        return authoritySet;
    }

    public void setAuthoritySet(Set<SystemAuthority> authoritySet) {
        this.authoritySet = authoritySet;
    }

    public boolean isSystemManager() {
        return systemManager;
    }

    public void setSystemManager(boolean systemManager) {
        this.systemManager = systemManager;
    }

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public void addAuthority(SystemAuthority systemAuthority) {
        this.authoritySet.add(systemAuthority);
    }

    public void removeAuthority(SystemAuthority systemAuthority) {
        this.authoritySet.remove(systemAuthority);
    }

    public Enterprise getEnterprise() {
        return enterprise;
    }

    public void setEnterprise(Enterprise enterprise) {
        this.enterprise = enterprise;
    }

}
